#!/usr/bin/env python
# yongming.li 

import sys
import os
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from  lmdd  import *

emmcPath = os.path.dirname(__file__)
print 'emmcPath is : ' + emmcPath
sys.path.append('../'+emmcPath)

#sys.path.append(os.getcwd())

from pychartdir import *
import threading
#(4k 8k 16k 32k 64k 128k 256k 512k 1m 2m 4m 8m 16m 32m 64m 128m 256m)
emmcLabels = ('16k','32k','64k','128k','256k','512k','1m','2m','4m','8m','16m','32m','64m','128m','256m')
def  createEMMCChart(myname,mydata,mydata2):
    # The data for the bar chart
    data0 = []
    data1 =[]
    for label in emmcLabels:
        data0.append(mydata[label])
        data1.append(mydata2[label])
    
    labels=emmcLabels;

    # Create a XYChart object of size 400 x 240 pixels
    c = XYChart(450, 240)

    # Add a title to the chart using 10 pt Arial font
    c.addTitle("         Test emmc %s speed"%(myname), "", 10)

    # Set the plot area at (50, 25) and of size 320 x 180. Use two alternative background
    # colors (0xffffc0 and 0xffffe0)
    c.setPlotArea(50, 25, 370, 180, 0xffffc0, 0xffffe0)

    # Add a legend box at (55, 18) using horizontal layout. Use 8 pt Arial font, with
    # transparent background
    c.addLegend(55, 18, 0, "", 8).setBackground(Transparent)

    # Add a title to the y-axis
    c.yAxis().setTitle("Throughput (MBytes Per sec)")

    # Reserve 20 pixels at the top of the y-axis for the legend box
    c.yAxis().setTopMargin(20)

    # Set the x axis labels
    c.xAxis().setLabels(labels)

    # Add a multi-bar layer with 3 data sets and 3 pixels 3D depth
    layer = c.addBarLayer2(Side, 3)
    layer.addDataSet(data0, 0xff8080, "first emmc")
    layer.addDataSet(data1, 0x80ff80, "second emmc")

    # Output the chart
    c.makeChart(emmcPath+"/emmc_%s.png"%(myname))
    #c.makeChart("emmc_%s.png"%(myname))


class EMMCTestThread(threading.Thread):

    def __init__(self,emmcform):
        threading.Thread.__init__(self)
        self.ui = emmcform

    def run(self):
        lines=[]
        finallyResult={}
        #for J in ('4k','8k','16k','32k','64k','128k','256k','512k','1m','2m'):
        for J in emmcLabels:
             testEMMC('read',J,25,lines,finallyResult) 
             self.ui.myUpdate(lines)
        print finallyResult


class EMMCForm(QDialog):

    def  showEMMCresult(self):
        self.resultbox = QtGui.QGroupBox("show first analyze result :")
        layout = QtGui.QVBoxLayout()
        self.resultsedit = QtGui.QTextEdit('')
        
        self.resultsedit.setTextColor(QColor(0, 0, 255, 255))

        self.resultsedit.setMinimumSize(300,500)
        layout.addWidget(self.resultsedit)

        self.buttonBeginEMMC = QtGui.QPushButton('begin test')
        layout.addWidget(self.buttonBeginEMMC)

        self.resultbox.show()
        self.resultbox.setLayout(layout)

    def  showEMMCChart(self):
        self.chartbox = QtGui.QGroupBox("contrast chart will show here  :")
        layout = QtGui.QVBoxLayout()
        self.chartlabel_write = QtGui.QLabel('')
        self.chartlabel_read = QtGui.QLabel('')
        

        layout.addWidget(self.chartlabel_write)
        layout.addWidget(self.chartlabel_read)

        self.chartbox.show()
        self.chartbox.setLayout(layout)
         
    def  showEMMCresult2(self):
        self.resultbox2 = QtGui.QGroupBox("show second analyze result :")
        layout = QtGui.QVBoxLayout()
        self.resultsedit2 = QtGui.QTextEdit('')
        
        self.resultsedit2.setTextColor(QColor(0, 0, 255, 255))

        self.resultsedit2.setMinimumSize(300,500)
        layout.addWidget(self.resultsedit2)

        self.buttonBeginEMMC2 = QtGui.QPushButton('begin test')
        layout.addWidget(self.buttonBeginEMMC2)

        self.resultbox2.show()
        self.resultbox2.setLayout(layout)

    def  myUpdate(self,lines):
         for line in lines:
             self.resultsedit.append(line)
         #self.repaint()

    def  beginEMMCTestRun(self,type,myedit,myresult):
        lines=[]
        for J in emmcLabels:
             testEMMC(type,J,10,lines,myresult) 
             for line in lines:
                 myedit.append(line)
        print myresult
                  
    def  beginEMMCTest(self):
         clickedButton = self.sender()
         if clickedButton == self.buttonBeginEMMC:
             print '1'
             self.beginEMMCTestRun('write',self.resultsedit,self.finallyResult_write)
             self.beginEMMCTestRun('read',self.resultsedit,self.finallyResult_read)
         if clickedButton == self.buttonBeginEMMC2:
             print '2'
             self.beginEMMCTestRun('write',self.resultsedit2,self.finallyResult2_write)
             self.beginEMMCTestRun('read',self.resultsedit2,self.finallyResult2_read)
         #self.emmcTestThread.start()
         if len(self.finallyResult_read)!=0 and len(self.finallyResult2_write)!=0:
             createEMMCChart('read',self.finallyResult_read,self.finallyResult2_read)
             createEMMCChart('write',self.finallyResult_write,self.finallyResult2_write)
             self.chartlabel_write.setPixmap(QPixmap.fromImage(QImage(emmcPath+'/emmc_write.png')))
             self.chartlabel_read.setPixmap(QPixmap.fromImage(QImage(emmcPath+'/emmc_read.png')))
         
         #startEMMC('write') 

    def  showfindbugsteps(self):

        self.showstepsbox = QtGui.QGroupBox("find native crush steps :")
        vlayout = QtGui.QVBoxLayout()
        hlayout = QtGui.QHBoxLayout()
        steps = """
=======================================================
1  find string like :
========================================================
""" 
        self.showstepsbox.show()
        self.showstepsbox.setLayout(vlayout)

    def __init__(self, parent=None):

        super(EMMCForm, self).__init__(parent)
        #self.myPath = os.path.dirname(__file__)
        #print self.myPath
        self.frameStyle = QtGui.QFrame.Sunken | QtGui.QFrame.Panel
        self.finallyResult_write={}
        self.finallyResult2_write={}
        self.finallyResult_read={}
        self.finallyResult2_read={}
   
        self.showEMMCresult()   
        self.showEMMCresult2()  
        self.showEMMCChart()

        self.emmcTestThread = EMMCTestThread(self)
  
        

        mainLayout = QGridLayout()
 
      
        self.setWindowTitle("EMMC Test tool")

        mainLayout.addWidget(self.resultbox,0,0)
        mainLayout.addWidget(self.chartbox,0,1)
        mainLayout.addWidget(self.resultbox2,0,2)
       
        
        self.connect(self.buttonBeginEMMC, SIGNAL('clicked()'), self.beginEMMCTest)
        self.connect(self.buttonBeginEMMC2, SIGNAL('clicked()'), self.beginEMMCTest)


        self.setLayout(mainLayout)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = None
    form = EMMCForm()
    sys.exit(app.exec_())




